// pages/teacher/teacher.js
import {
  request
} from '../../utils/util.js'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    commentList: [],
    has_more: true,
    show_modal: false,
    tab_index: 0, //0：预约，1：简介
    course_index: 0, //选中课程
    course_id: null,
    day_index: 0,
    today: '2019-03-13',
    timeList: [],
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      day_index: options.day_index,
      course_id: options.course_id,
      teacher_id: options.id
    })

    this.getDayList();
    this.getTeacherDetail();
  },

  //tap切换事件
  changeTap: function(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      tab_index: index
    })

    if (index == 1 && this.data.commentList.length == 0) {
      this.getTeacherCommnet();
    }
  },

  //改选课程
  changeCourse: function(e) {
    let index = e.currentTarget.dataset.index;
    let id = e.currentTarget.dataset.id;
    this.setData({
      course_index: index,
      course_id: id
    })
    this.getTimeList();
  },

  // 获取预约时段列表
  getTimeList: function(){
    let that = this
    let { teacher_id, today, course_id, timeList, day_index } = that.data
    request({
      url: 'Teacher/bookCourseTimeList',
      method: 'GET',
      data: {
        teacher_id,
        date: today,
        course_id,
      },
      success: data => {
        timeList[day_index] = data
        that.setData({
          timeList,
        })
      }
    })
  },

  //获取教师详情&可预约课程
  getTeacherDetail: function() {
    let that = this;
    let { teacher_id, today, course_id, course_index } = that.data
    request({
      url: 'Teacher/bookTeacherCourse',
      method: 'GET',
      data: {
        teacher_id,
        date: today
      },
      success: data => {
        let courseList = data.teacher_course
        for (let i = 0; i < courseList.length; i++){
          course_index = courseList[i].course_id == course_id ? i : 0
        }
        that.setData({
          teacher: data.teacher,
          courseList,
          course_index
        })
        this.getTimeList()
      }
    })
  },

  //获取7天日期
  getDayList: function() {
    let dayList = [];
    let timeList = [];
    for (let i = 0; i < 7; i++) {
      let day = this.getDay(i);
      if (i == 0) {
        day.week = '今';
        this.setData({
          today: day.date
        })
      } else if (i == 1) {
        day.week = '明'
      }
      dayList.push(day)
      timeList.push([])
    }

    let day_index = this.data.day_index;

    this.setData({
      dayList,
      today: dayList[day_index].date,
      timeList
    })
  },

  //获取日期
  getDay: function(day) {
    let today = new Date();
    let targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
    today.setTime(targetday_milliseconds); //注意，这行是关键代码
    let tYear = today.getFullYear();
    let tMonth = today.getMonth();
    let tDate = today.getDate();
    tMonth = (tMonth + 1) < 10 ? ("0" + (tMonth + 1)) : (tMonth + 1);
    tDate = tDate < 10 ? ("0" + tDate) : tDate;
    let time = {
      date: tYear + "-" + tMonth + "-" + tDate,
      day: tDate,
      week: "日一二三四五六".charAt(today.getDay()),
      month: tMonth,
    }
    return time;
  },

  //日期改变
  changeDay(e) {
    let { index } = e.currentTarget.dataset
    this.setData({
      day_index: index,
    })
  },

  //日期改变
  dayChange: function () {
    let { dayList, day_index } = this.data
    this.setData({
      today: dayList[day_index].date,
    })
    this.getTimeList();
  },

  //获取教师评论列表
  getTeacherCommnet: function() {
    let that = this;
    request({
      url: 'Comment/commentTeacher',
      method: 'GET',
      data: {
        teacher_id: that.data.teacher_id,
        page: that.data.page,
      },
      success: data => {
        let has_more = true;
        if (data.list.length < 10) {
          has_more = false;
        }
        that.setData({
          commentList: [...that.data.commentList, ...data.list],
          commentCount: data.count,
          score: data.score,
          has_more
        })
      }
    })
  },

  //加载更多
  getMore: function() {
    this.data.page++;
    this.getTeacherCommnet();
  },

  //打开弹框
  openModal: function(e) {
    let item = e.currentTarget.dataset.item;
    if(!item.is_book&&item.book_num>=item.limit){
      item.is_standby = true
    }
    this.setData({
      show_modal: true,
      time_item: item
    })
  },

  //关闭弹框
  closeModal: function(e) {
    if (e.currentTarget.dataset.is_close == 1) {
      this.setData({
        show_modal: false
      })
    }
  },

  //确认预约
  confirmBook: function() {
    let that = this;

    let {
      teacher_id,
      courseList,
      time_item,
      course_id,
    } = this.data;

    request({
      url: 'User/confirmBookTime',
      method: 'POST',
      data: {
        teacher_id: teacher_id,
        time_id: time_item.id,
        course_id,
      },
      success: data => {
        this.setData({
          show_modal: false,
        })
        wx.showModal({
          title: '温馨提示',
          content: '预约成功',
          showCancel: false,
          success: function(res) {
            let messInfo = app.globalData.messInfo
            wx.requestSubscribeMessage({
              tmplIds: [messInfo.course_cancel_sub, messInfo.course_notify_sub],
            })
            that.getTeacherDetail();
          },
        })
      }
    })
  },

  // 左右滑动切换
  swiperChange(e) {
    let index = e.detail.current
    this.setData({
      day_index: index,
    })
    this.dayChange()
  },

  // 候补预约
  standbyBook: function(){
    let that = this
    let {
      time_item,
    } = that.data;

    request({
      url: 'User/addCandidate',
      method: 'POST',
      data: {
        time_id: time_item.id,
      },
      success: data => {
        that.setData({
          show_modal: false,
        })
        wx.showModal({
          content: '已提交候补预约，候补成功将自动预约课程',
          showCancel: false,
          confirmText: '好的',
          success (res) {
            let messInfo = app.globalData.messInfo
            wx.requestSubscribeMessage({
              tmplIds: [messInfo.course_candidate_sub,messInfo.course_cancel_sub, messInfo.course_notify_sub],
            })
            that.getTimeList()
          }
        })
      }
    })
  },

  // 取消候补
  standbyCancel: function(){
    let that = this
    let {
      time_item,
    } = that.data;
    request({
      url: 'User/cancelCandidate',
      method: 'POST',
      data: {
        time_id: time_item.id,
      },
      success: data => {
        that.setData({
          show_modal: false,
        })
        wx.showModal({
          content: '已取消候补预约',
          showCancel: false,
          confirmText: '好的',
          success (res) {
            that.getTimeList()
          }
        })
      }
    })
  }

})